class AccountController < ApplicationController
  
  before_filter :login_required, :except => [:login]
#    rescue_from Exception, :with => :render_error
#
#  def render_error(exception)
#    logger.info "User logged : #{current_user.inspect}"
#    log_error(exception)
#    render :template => "/errors/error.html.erb"
#  end

  def login
    redirect_to :controller => '/home', :action => 'index' and return if logged_in?
    return unless request.post?
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        self.current_user.remember_me
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      self.current_user.set_last_login
      redirect_back_or_default(:controller => '/home', :action => 'index')
      logger.info "User logged in succesfully. User : #{params[:login]}"
      #flash[:notice] = "Logged in successfully"
      flash[:notice] = _('Logged in successfully')
    else
      logger.info "User can't log in. User : #{params[:login]}"
      flash[:notice] = _("Invalid Username / Password combination")
    end
  end

#  Signup functionality disabled
#  def signup
#    @user = User.new(params[:user])
#    return unless request.post?
#    @user.save!
#    self.current_user = @user
#    redirect_back_or_default(:controller => '/account', :action => 'index')
#    flash[:notice] = "Thanks for signing up!"
#  rescue ActiveRecord::RecordInvalid
#    render :action => 'signup'
#  end
  
  def logout
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = _("You have been logged out.")
    logger.info "User logged out. User : #{params[:login]}"
    redirect_back_or_default(:controller => '/home', :action => 'index')
  end
end
